﻿using System;
using System.Runtime.InteropServices;
using LizardSoft.Common.FlexibleStructureDeployment;
using Microsoft.SharePoint;

namespace SP.FlexibleStructureDeployment.Test.MyShop.Features.StructureWebs
{
    [Guid("eaac0818-33b1-4f89-98e6-cbcb9e4af103")]
    public class StructureWebsEventReceiver : SPFeatureReceiver
    {
        public override void FeatureActivated(SPFeatureReceiverProperties properties)
        {
            SPSite site = properties.Feature.Parent as SPSite;

            ProcessOnRootWeb(site.Url, String.Empty);
            ProcessOnDealsWeb(site.Url, "deals");
            ProcessOnTasksWeb(site.Url, "tasks");
        }

        private static void ProcessOnRootWeb(string siteUrl, string webStrUrl)
        {
            SPContentTypeId productCategory = new SPContentTypeId("0x010094bf1f2a2315406782b9e57a7814c6aa");
            SPContentTypeId city = new SPContentTypeId("0x01008fa7d59b82b445cfaa72a3eeb1dbebdb");
            SPContentTypeId country = new SPContentTypeId("0x0100ab8e89b0b24d4b2390fdede10c727edf");
            SPContentTypeId businessType = new SPContentTypeId("0x01008eed4cf6ade2499cb591a3057d2d1b6e");

            StructureHelper.ProcessList(siteUrl, webStrUrl, new ListProcessingInfo()
            {
                ListFolderUrl = "Lists/ProductCategories",
                ListInternalName = "ProductCategories",
                ListTitle = "Категории продуктов",
                ListDescription = String.Empty,
                TemplateType = SPListTemplateType.GenericList,
                ShowOnQuickLaunch = true,
                SiteContentTypeIds = new[] { productCategory }
            });

            StructureHelper.ProcessList(siteUrl, webStrUrl, new ListProcessingInfo()
            {
                ListFolderUrl = "Lists/Cities",
                ListInternalName = "Cities",
                ListTitle = "Города",
                ListDescription = String.Empty,
                TemplateType = SPListTemplateType.GenericList,
                ShowOnQuickLaunch = true,
                SiteContentTypeIds = new[] { city }
            });

            StructureHelper.ProcessList(siteUrl, webStrUrl, new ListProcessingInfo()
            {
                ListFolderUrl = "Lists/Countries",
                ListInternalName = "Countries",
                ListTitle = "Страны",
                ListDescription = String.Empty,
                TemplateType = SPListTemplateType.GenericList,
                ShowOnQuickLaunch = true,
                SiteContentTypeIds = new[] { country }
            });

            StructureHelper.ProcessList(siteUrl, webStrUrl, new ListProcessingInfo()
            {
                ListFolderUrl = "Lists/BusinessTypes",
                ListInternalName = "BusinessTypes",
                ListTitle = "Бизнес-типы",
                ListDescription = String.Empty,
                TemplateType = SPListTemplateType.GenericList,
                ShowOnQuickLaunch = true,
                SiteContentTypeIds = new[] { businessType }
            });

            StructureHelper.CorrectSiteLookupFields(siteUrl, new[]{
                "CustomerBusinessType",
                "CustomerCity",
                "CustomerCity_MapCode",
                "ProductCategory",
                "CustomerCountry"
            });

            SPContentTypeId customer = new SPContentTypeId("0x01002a50a4b20a8f457cbd6a26e1c224d092");
            SPContentTypeId customerInternal = new SPContentTypeId("0x01002a50a4b20a8f457cbd6a26e1c224d09200c3a8fb927e754ecb99e8dbe24d949825");
            SPContentTypeId customerForeign = new SPContentTypeId("0x01002a50a4b20a8f457cbd6a26e1c224d09200b83d4b97980948aa8866cf229b0f7821");
            SPContentTypeId product = new SPContentTypeId("0x01008b60a4412bed4d99bb0bb2d8843e2cfb");

            StructureHelper.ProcessList(siteUrl, webStrUrl, new ListProcessingInfo()
            {
                ListFolderUrl = "Lists/Customers",
                ListInternalName = "Customers",
                ListTitle = "Клиенты",
                ListDescription = String.Empty,
                TemplateType = SPListTemplateType.GenericList,
                ShowOnQuickLaunch = true,
                SiteContentTypeIds = new[] { customerInternal, customerForeign }
            });

            StructureHelper.ProcessList(siteUrl, webStrUrl, new ListProcessingInfo()
            {
                ListFolderUrl = "Lists/Products",
                ListInternalName = "Products",
                ListTitle = "Продукция",
                ListDescription = String.Empty,
                TemplateType = SPListTemplateType.GenericList,
                ShowOnQuickLaunch = true,
                SiteContentTypeIds = new[] { product }
            });

            StructureHelper.CorrectSiteLookupFields(siteUrl, new[]{
                "CustomerName",
                "ProductName"
            });
        }

        private static void ProcessOnDealsWeb(string siteUrl, string webStrUrl)
        {
            SPContentTypeId deal = new SPContentTypeId("0x010028ff5c9db4fb4bfa9cc55988183fa432");

            StructureHelper.ProcessList(siteUrl, webStrUrl, new ListProcessingInfo()
            {
                ListFolderUrl = "Lists/Deals",
                ListInternalName = "Deals",
                ListTitle = "Сделки",
                ListDescription = String.Empty,
                TemplateType = SPListTemplateType.GenericList,
                ShowOnQuickLaunch = true,
                SiteContentTypeIds = new[] { deal }
            });

            StructureHelper.CorrectSiteLookupFields(siteUrl, new[]{
                "DealID"
            });

            SPContentTypeId document = new SPContentTypeId("0x010100d15f623d1faf429ab3b607462d553e59");
            SPContentTypeId comment = new SPContentTypeId("0x011100494831b2f73f42ccac6c9ceb1b978fc4");

            StructureHelper.ProcessList(siteUrl, webStrUrl, new ListProcessingInfo()
            {
                ListFolderUrl = "DealDocuments",
                ListInternalName = "DealDocuments",
                ListTitle = "Документы по сделкам",
                ListDescription = String.Empty,
                TemplateType = SPListTemplateType.DocumentLibrary,
                ShowOnQuickLaunch = true,
                SiteContentTypeIds = new[] { document }
            });

            StructureHelper.ProcessList(siteUrl, webStrUrl, new ListProcessingInfo()
            {
                ListFolderUrl = "Lists/DealComments",
                ListInternalName = "DealComments",
                ListTitle = "Комментарии",
                ListDescription = String.Empty,
                TemplateType = SPListTemplateType.GenericList,
                ShowOnQuickLaunch = true,
                SiteContentTypeIds = new[] { comment }
            });
        }

        private static void ProcessOnTasksWeb(string siteUrl, string webStrUrl)
        {
            SPContentTypeId task = new SPContentTypeId("0x010800bee580dc8185429ca6eaaaa5a23bece6");

            StructureHelper.ProcessList(siteUrl, webStrUrl, new ListProcessingInfo()
            {
                ListFolderUrl = "Lists/DealTasks",
                ListInternalName = "DealTasks",
                ListTitle = "Задачи по сделкам",
                ListDescription = String.Empty,
                TemplateType = SPListTemplateType.Tasks,
                ShowOnQuickLaunch = true,
                SiteContentTypeIds = new[] { task }
            });
        }
    }
}
